package implementation.menu {
import palf.commands.Command;
import palf.commands.CommandFactory;
import palf.menu.Menu;
import palf.menu.MenuBuilder;
import palf.menuItems.MenuItem;
import palf.menuItems.MenuItemFactory;

/* takes MenuContent and constructs pages */

internal class MenuBuilderImplementation implements MenuBuilder {

	private const ITEM_FACTORY : MenuItemFactory = new MenuItemFactory();
	private const COMMAND_FACTORY : CommandFactory = new CommandFactory();

	public function MenuBuilderImplementation() {

	}

	public function setContent(menu : Menu, content : MenuContent) : void {
		var main_content : Vector.<MenuItem> = createPage(content.getMainPageContent());
		var layout_content : Vector.<MenuItem> = createPage(content.getLayoutPageContent());
		var circular_content : Vector.<MenuItem> = createPage(content.getCircularPageContent());
		var alignment_content : Vector.<MenuItem> = createPage(content.getAlignmentPageContent());
		var transition_content : Vector.<MenuItem> = createPage(content.getTimingPageContent());

		var load_main_page : Command = COMMAND_FACTORY.navigateMenu(menu, main_content);
		var load_layout_page : Command = COMMAND_FACTORY.navigateMenu(menu, layout_content);
		var load_circular_page : Command = COMMAND_FACTORY.navigateMenu(menu, circular_content);
		var load_alignment_page : Command = COMMAND_FACTORY.navigateMenu(menu, alignment_content);
		var load_transition_page : Command = COMMAND_FACTORY.navigateMenu(menu, transition_content);

		addLinkToPage(layout_content, "Circular", load_circular_page);
		addLinkFromPage(circular_content, "Back", load_layout_page);

		addLinkToPage(layout_content, "Alignment", load_alignment_page);
		addLinkFromPage(alignment_content, "Back", load_layout_page);

		addLinkFromPage(layout_content, "Back", load_main_page);
		addLinkToPage(main_content, "Layout", load_layout_page);

		addLinkToPage(main_content, "Transition", load_transition_page);
		addLinkFromPage(transition_content, "Back", load_main_page);

		main_content.push(ITEM_FACTORY.createMenuDivider());
		load_main_page.execute();
	}

	private function addLinkToPage(page_content : Vector.<MenuItem>, label : String, load_page : Command) : void {
		var button : MenuItem = ITEM_FACTORY.createMenuButton(label, load_page);
		page_content.push(button);
	}

	private function addLinkFromPage(page_content : Vector.<MenuItem>, label : String, load_page : Command) : void {
		page_content.push(ITEM_FACTORY.createMenuDivider());
		addLinkToPage(page_content, label, load_page);
	}

	private function createPage(items : Array) : Vector.<MenuItem> {
		var content : Vector.<MenuItem> = Vector.<MenuItem>([]);
		var item : MenuItem;
		for each (var object : Object in items) {
			item = createMenuItemFromContentData(object);
			content.push(item);
		}
		return content;
	}

	private function createMenuItemFromContentData(object : Object) : MenuItem {
		switch (object.type) {
			//TODO - don't use strings!
			case "divider" :
				return ITEM_FACTORY.createMenuDivider();
				break;

			case "header" :
				return ITEM_FACTORY.createMenuLabel(object.label);
				break;

			case "button" :
				return ITEM_FACTORY.createMenuButton(object.label, object.command);
				break;

			default :
				return ITEM_FACTORY.createMenuDivider();
				break;
		}
	}

}

}
